﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using ExtBusiness;
using System.Configuration;
using ExtBusiness.Interface;
using ExtBusiness.User;
using Common;
using ExtBusiness.CommonBusiness;
using ChBusiness.CommonBusiness;
using ChBusiness;

public partial class WebPubManager_UserEdit :PageBase
{
    private UserManager cUser = new UserManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //设置身份证类型的验证
            ddlZjlx.ValidatorName = cvZjhm.ClientID;
            Page.ClientScript.RegisterStartupScript(this.GetType(), "knh1", "<script>ddlChange('" + ddlZjlx.ddlClientID + "');</script>");

            if (!string.IsNullOrEmpty(Request["id"]))
            {
                DataTable dt = cUser.GetUser(Request["id"]);

                if (dt.Rows.Count > 0)
                {
                    txtUserId.Text =BasicOperate.GetDataBaseString(dt.Rows[0]["USERID"]);
                    txtUserName.Text = BasicOperate.GetDataBaseString(dt.Rows[0]["USERNAME"]);
                    trUserPwd.Visible = false;
                    trUserRePwd.Visible = false;
                    ddlZjlx.Value = dt.Rows[0]["IDTYPE"].ToString();
                    txtZjhm.Text = dt.Rows[0]["IDNUMBER"].ToString();
                    txtTel.Text = dt.Rows[0]["TEL"].ToString();
                    txtCz.Text = dt.Rows[0]["FAX"].ToString();
                    txtMobile.Text = dt.Rows[0]["MOBILE"].ToString();
                    txtEmail.Text = dt.Rows[0]["EMAIL"].ToString();
                    txtYZBM.Text = dt.Rows[0]["POSTCODE"].ToString();
                    txtAddress.Text = dt.Rows[0]["ADDRESS"].ToString();
                    hfUsertype.Value = dt.Rows[0]["type"].ToString();//用户类型
                }
            }
            //单位ID和单位名称
            if ((!string.IsNullOrEmpty(Request["unitsid"])) && (!string.IsNullOrEmpty(Request["unitsname"])))
            {
                hidUnitsID.Value = Request["unitsid"];
                lblUntisName.Text = Request["unitsname"];
            }
            else
            {
                trUnitsName.Visible = false;
            }
        }
    }

    protected void btnRefer_Click(object sender, EventArgs e)
    {
        //获取操作者ip地址
        string myIP = Common.IPOperation.GetIpAddress();

        string strId = Request["id"];
        string strCszcUserType = ConfigurationManager.AppSettings["csyhSystemFlag"];
        if (string.IsNullOrEmpty(strId))
        {
            if (HiddenFieldFlag.Value == "0")
            {
                return;
            }
            else if (HiddenFieldFlag.Value == "1")
            {
                return;
            }
            else if (HiddenFieldFlag.Value == "2")
            {
                return;
            }
            else if (HiddenFieldFlag.Value == "3")
            {
                return;
            }
            else
            {
                bool userExist = UserManager.CheckUserExist(txtUserId.Text);
                if (!userExist)
                {
                    UserManager.CreateUser(txtUserId.Text, txtUserName.Text, txtUserPwd.Text,
                        int.Parse(strCszcUserType), txtTel.Text, txtMobile.Text,
                        txtAddress.Text, ddlZjlx.Value, txtZjhm.Text, txtCz.Text, txtEmail.Text,
                        txtYZBM.Text,hidUnitsID.Value);

                    //添加操作日志 addby zhongjian 20100421
                    string strRemark = "添加用户信息。登录名称: " + txtUserId.Text +"用户名称:"+ txtUserName.Text;
                    SystemLogs.AddSystemLogs(Session["UserName"].ToString(), "add", strRemark, "",myIP);

                    Response.Redirect("UserList.aspx?unitsid=" + hidUnitsID.Value + "&unitsname=" + lblUntisName.Text + "");
                }
                else
                {
                    labMsg.Text = "用户名已存在，请重新输入！";
                }
            }
        }
        else
        {
            strCszcUserType = hfUsertype.Value;//在作修改时,应该保存原有的用户类型 addby zhongjian 20090904
            UserManager.UpdateUser(strId, txtUserId.Text, txtUserName.Text,
                int.Parse(strCszcUserType), txtTel.Text, txtMobile.Text,
                txtAddress.Text, ddlZjlx.Value, txtZjhm.Text, txtCz.Text, txtEmail.Text,
                txtYZBM.Text);

            //添加操作日志 addby zhongjian 20100421
            string strRemark = "修改用户信息。登录名称: " + txtUserId.Text + "用户名称:" + txtUserName.Text;
            SystemLogs.AddSystemLogs(Session["UserName"].ToString(), "update", strRemark, "",myIP);

            //更新用户信息            
            InterfaceOperation infaceOperation = new InterfaceOperation();
            string strXml = infaceOperation.GetUserInfoXml(strId,"update");

            if (!string.IsNullOrEmpty(strXml))
            {
                try
                {
                    DxInterface.IdsService idsTemp = new DxInterface.IdsService();

                    string strCheckCode = Encode.Md5(DateTime.Now.ToShortDateString());
                    string strReturn = idsTemp.UserInfo(strXml, strCheckCode);

                    if (!string.IsNullOrEmpty(strReturn))
                    {
                        XmlHandle xhTemp = new XmlHandle();
                        if (!xhTemp.HaveTargetString(strReturn, "UserInfoResponse", "true"))                        
                        {
                            infaceOperation.UpdateExceptionAndXml(strXml, "接口更新失败",10);
                        }
                    }
                    else 
                    {
                        infaceOperation.UpdateExceptionAndXml(strXml, "接口返回值为空",10);
                    }
                }
                catch (Exception ex)
                {
                    infaceOperation.UpdateExceptionAndXml(strXml, ex.Message,10);
                }
            }
        }
    }

    #region 返回菜单管理主界面
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserList.aspx?unitsid=" + hidUnitsID.Value + "&unitsname=" + lblUntisName.Text + "");
    }
    #endregion
}
